package edu.school.controller;

import com.github.pagehelper.PageInfo;
import edu.school.entity.Comment;
import edu.school.entity.Reply;
import edu.school.entity.User;
import edu.school.service.CommentService;
import edu.school.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ReplyController {
    @Autowired
    ReplyService service;
    @Autowired
    CommentService commentService;
    @RequestMapping(value = "/reply/toAdd")
    public String toAdd() {
        return "client/add_reply";
    }
//    @RequestMapping(value = "/reply/add",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
//    @ResponseBody
//    public String add(Reply record,HttpSession session){
//        User user = (User) session.getAttribute("user");
//        if (null==user){
//            return "<script>alert('请先登录');location.href='/toLogin.action';</script>";
//        }
//        Date date = new Date();
//        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
//        Timestamp timeStamp = new Timestamp(date.getTime());
//        record.setCreate_time(timeStamp);
//        record.setUid(user.getId());
//        if(service.save(record)){
//            return "<script>alert('发布成功');location.href='/reply/list';</script>";
//            /*  return "<script>alert('注册成功');location.href='/toLogin.action';</script>";*/
//        }else{
//            return "<script>alert('发布失败');history.go(-1);</script>";
//        }
//
//    }
@RequestMapping("/reply/add")
@ResponseBody
public Map<String, Object> addReply(Reply record, HttpSession session) {
    Map<String, Object> result = new HashMap<>();
    User user = (User) session.getAttribute("user");
    Comment comment = commentService.findById(record.getCid());

    if (user == null) {
        result.put("success", false);
        result.put("message", "用户未登录");
    } else if (comment.getUid() == user.getId()) {
        result.put("success", false);
        result.put("message", "不能回复自己的评论");
    } else {
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        record.setCreate_time(timeStamp);
        record.setUid(user.getId());
        service.save(record);
        result.put("success", true);
        result.put("message", "回复成功");
    }

    return result;
}
    @RequestMapping(value = "/reply/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList(Reply record,HttpSession session,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){
        User user=(User) session.getAttribute("user");
        if(user!=null){
            record.setUid(user.getId());
        }
        PageInfo<Reply> pageInfo = service.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        if(user!=null){
            modelMap.addAttribute("url","reply");
            return "/client/reply_list";
        }
        return "/reply/list";
    }
    @RequestMapping(value = "/reply/list1",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList1(Reply record,HttpSession session,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){
        PageInfo<Reply> pageInfo = service.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        return "/reply/list";
    }
    @RequestMapping(value = "/reply/query")
    public String query( Integer id, Model model){
        Reply record=service.findById(id);
        if(null!=record){
            model.addAttribute("record",record);
        }
        return "reply/edit";
    }

    //前台详情页
    @RequestMapping(value = "/reply/detail")
    public String qtdetail(Integer id, Model model) {
        Reply record = service.findById(id);
        if (null != record) {
            model.addAttribute("record", record);
        }
        return "client/reply_detail";
    }
    //公告修改功能
    @RequestMapping(value = "/reply/edit",method = {RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String edit(Reply record,HttpSession session){
        if(service.update(record)){
            return "<script>alert('修改成功');location.href='/reply/list';</script>";

        }else{
            return "<script>alert('修改失败');history.go(- 1);</script>";
        }

    }


    @RequestMapping("/reply/delete")
    @ResponseBody
    public String ajaxdelete(Integer id) {
        boolean b =service.deleteReply(id);

        if(b){
            return "ok";
        }
        return "fail";

    }
    //批量删除
    @RequestMapping("/reply/deletemany")
    @ResponseBody
    public String  deletemany(String ids) {
        System.err.println(ids);
        String[] arrayids=ids.split(",");//把数组里的值逗号隔开
        System.out.println("批量删除成功");

        service.deleteMany(arrayids);
        return "ok";//返回给ajax



    }
}
